<template>
	<sc-dialog
		v-model="visible"
		show-fullscreen
		:title="titleMap[mode]"
		width="700px"
		heigit="700px"
		destroy-on-close
		@close="close"
	>
		<el-alert
			title="选择全部表后，选择的将不生效"
			type="success"
			:closable="false"
		></el-alert>
		<el-form
			ref="elForm"
			:model="formData"
			:rules="rules"
			size="medium"
			label-width="100px"
			style="margin-top: 20px"
		>
			<el-form-item label="命名空间" prop="namespace">
				<el-input
					v-model="formData.namespace"
					placeholder="例如：Sys"
					:maxlength="30"
					show-word-limit
					clearable
					prefix-icon="el-icon-collection-tag"
					:style="{ width: '100%' }"
				></el-input>
			</el-form-item>
			<el-form-item label="生成类型" prop="types">
				<el-radio-group v-model="formData.types" size="medium">
					<el-radio
						v-for="(item, index) in typesOptions"
						:key="index"
						:label="item.value"
						:disabled="item.disabled"
					>
						{{ item.label }}
					</el-radio>
				</el-radio-group>
			</el-form-item>
			<el-form-item label="API版本" prop="apiVersion">
				<el-input
					v-model="formData.apiVersion"
					placeholder="例如：v1"
					:maxlength="20"
					show-word-limit
					clearable
					prefix-icon="el-icon-discount"
					:style="{ width: '100%' }"
				></el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="close">取 消</el-button>
			<el-button :loading="isSaveing" type="primary" @click="save">
				确 定
			</el-button>
		</template>
	</sc-dialog>
</template>
<script>
export default {
	data() {
		return {
			mode: "add",
			titleMap: {
				add: "生成代码",
			},
			isSaveing: false,
			visible: false,
			formData: {
				tableNames: [],
				namespace: "",
				types: 2,
				apiVersion: "",
			},
			rules: {
				namespace: [
					{
						required: true,
						message: "例如：Sys",
						trigger: "blur",
					},
				],
				types: [
					{
						required: true,
						message: "单选框组不能为空",
						trigger: "change",
					},
				],
				apiVersion: [
					{
						required: true,
						message: "例如：v1",
						trigger: "blur",
					},
				],
			},
			typesOptions: [
				{
					label: "全部表",
					value: 1,
				},
				{
					label: "部分表",
					value: 2,
				},
			],
		};
	},
	methods: {
		async save() {
			this.isSaveing = true;
			const res = await this.$API.modulesgenerate.code.post(
				this.formData
			);
			this.isSaveing = false;
			if (res.code == 200) {
				this.$message.success(
					"生成成功，到根目录【Generator】文件夹中查看"
				);
			} else {
				this.$alert(res.message, "提示", { type: "error" });
			}
		},
		open(row) {
            row.forEach(element => {
                this.formData.tableNames.push(element.name)
            });
			this.visible = true;
		},
		close() {
			this.visible = false;
		},
	},
};
</script>
